package com.ciwong.epaper.modules.me.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciwong.mobilelib.utils.ViewHolder;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.ui.OfflineBookActivity;
import com.ciwong.epaper.modules.epaper.bean.EpaperInfo;
import com.ciwong.epaper.util.FileUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;

import java.util.List;

/**
 * Created by res-jianpingliu on 2015/8/11.
 */
public class OfflineBookAdapter extends BaseAdapter
{
    private Context mContext;

    private List<EpaperInfo> mList;

    private DisplayImageOptions options;

    public OfflineBookAdapter(Context context,
                              List<EpaperInfo> offlineFiles)
    {
        mContext = context;
        mList = offlineFiles;
        options = new DisplayImageOptions.Builder().cacheInMemory(true)
                .cacheOnDisk(true).showImageForEmptyUri(R.mipmap.item_load_icon)
                .showImageOnFail(R.mipmap.item_load_icon).build();
    }

    @Override
    public int getCount()
    {
        if (mList != null && !mList.isEmpty())
        {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position)
    {
        if (mList != null && !mList.isEmpty())
        {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View view;
        if (convertView == null)
        {
            view = View.inflate(mContext,
                    R.layout.adapter_offline_directory_item, null);
        }
        else
        {
            view = convertView;
        }
        TextView titleTV = ViewHolder.get(view, R.id.item_offline_title_tv);
        ImageView imageView = ViewHolder.get(view, R.id.item_offline_iv);
        TextView sizeTV = ViewHolder.get(view, R.id.item_offline_size_tv);
        TextView stageTV = ViewHolder.get(view, R.id.item_offline_stage_tv);
        View freeView = ViewHolder.get(view, R.id.offline_item_free);
        View kaoShi= ViewHolder.get(view,R.id.kao_shi);

        EpaperInfo offlineFile = (EpaperInfo) getItem(position);
        freeView.setVisibility(
                offlineFile.getIsFree() == 1 ? View.VISIBLE : View.GONE);
        titleTV.setText(offlineFile.getProductName());
        sizeTV.setText(FileUtil.getFileLength(offlineFile.getLength()));
        stageTV.setText(offlineFile.getSize() + "期资源");
        kaoShi.setVisibility(offlineFile.getRequired()==1?View.VISIBLE:View.GONE);
        ImageLoader.getInstance().displayImage(offlineFile.getCover(),
                new ImageViewAware(imageView), options);
        return view;
    }

    @Override
    public void notifyDataSetChanged()
    {
        if (mContext instanceof OfflineBookActivity)
        {
            if (mList != null && mList.size() == 0)
            {
                ((OfflineBookActivity) mContext).showLlEmpty();
            }
            else
            {
                ((OfflineBookActivity) mContext).hideLlEmpty();
            }
        }
        super.notifyDataSetChanged();
    }
}
